package com.yyk.yykywd.mvvm.view_model

import androidx.lifecycle.MutableLiveData
import com.orhanobut.logger.Logger
import com.yyk.yykywd.*
import com.yyk.yykywd.bean.BaseBean
import com.yyk.yykywd.mvvm.entity.AgentDetail1Bean
import com.yyk.yykywd.mvvm.entity.AgentDetailBean
import com.yyk.yykywd.mvvm.entity.HospitalDetailBean
import com.yyk.yykywd.mvvm.model.GetBase
import com.yyk.yykywd.mvvm.model.ReturnMoneyModel

/**
 * 文件描述：
 * Created by guo on 2019-11-19.
 */
class WorkDetailViewModel : BaseViewModel() {
    var id: String = ""
    //代理站点id
    var pact_id:String = ""
    //当前所在的代理站点的位置
    var position = 0
    //代理站点名称
    var name = ""
    var type: String = ""
    var flag = 0
    var showType = 0
    var typeS = 0
    var amountCollected = ""
    //代理商企业营业执照
    var licences: MutableList<String> = ArrayList()
    //合同图片
    var contracts: ArrayList<String> = ArrayList()
    //调研表
    var surveyForm: ArrayList<String> = ArrayList()
    //医疗机构许可证
    var permit: ArrayList<String> = ArrayList()
    //全款合同图片
    var allContracts: ArrayList<String> = ArrayList()
    var idFront: String = ""
    var idBack: String = ""

    private val model: ReturnMoneyModel = ReturnMoneyModel()

    //LiveData,使该数据可以被感知
    var agentDetail = MutableLiveData<AgentDetailBean>()
    var agentDetail1 = MutableLiveData<AgentDetail1Bean>()
    var hospitalDetail = MutableLiveData<HospitalDetailBean>()

    var visitorName = ""
    var visitorPosition = ""
    var visitorPhone = ""

    var isPerson: Boolean = true

    //是代理商详情还是医院详情
    var detail: Int = 0
    //仍需回款金额
    var stillNeedMoney = 0
    //已回款金额
    var alreadyMoney = 0

    fun getReturnMoneyDetail() {
        if (type == FROM_POTENTIAL_CUSTOMER_HOSPITAL ||
                type == FROM_C_CUSTOMER_HOSPITAL ||
                type == FROM_SIGN_CONTRACT_HOSPITAL ||
                type == FROM_CUSTOMER_OPEN_SEA_HOSPITAL ||
                type == FROM_INSTALL_HOSPITAL) {
            model.getHosDetail(id, object : ReturnMoneyModel.GetHospitalDetail {
                override fun getInfo(hospitalDetailBean: HospitalDetailBean) {
                    hospitalDetailBean.data?.run {
                        visitorName = call_name
                        visitorPosition = call_grade
                        visitorPhone = call_phone
                    }
                    hospitalDetail.value = hospitalDetailBean
                }

                override fun onError(e: String) {
                }

            })
            return
        }
        model.getAgentDetail(id, object : ReturnMoneyModel.GetAgentDetail {
            override fun getInfo(agentDetail: AgentDetailBean) {
                agentDetail.data?.let {
                    stillNeedMoney = it.wait_money
                    alreadyMoney = it.already_money
                }
                this@WorkDetailViewModel.agentDetail.value = agentDetail
            }

            override fun onError(e: String) {
            }

        })
    }

    //还能退款的合同数量
    var refund_num = 0
    //还能添加回款的合同数量
    var back_money_num = 0
    fun getAgentDetail() {
        model.getAgentDetail1(id, object : ReturnMoneyModel.GetAgentDetail1 {
            override fun getInfo(agentDetail: AgentDetail1Bean) {
                agentDetail.data?.let {
                    this@WorkDetailViewModel.refund_num = it.refund_num
                    this@WorkDetailViewModel.back_money_num = it.back_money_num
                }
                this@WorkDetailViewModel.agentDetail1.value = agentDetail
            }

            override fun onError(e: String) {
            }

        })
    }

    fun setC(from: String) {
        model.setC(from, detail.toString(), id, object : GetBase {
            override fun getInfo(base: BaseBean) {
                setBaseValue(base)
            }

            override fun onError(e: String) {
            }
        })
    }

    fun overAgencyPrice(amount: Int): Boolean {
        Logger.e(stillNeedMoney.toString())
        if (amount > stillNeedMoney) {
            return true
        }
        return false
    }

    fun equalAgencyPrice(amount: Int): Boolean {
        if (amount == stillNeedMoney) {
            return true
        }
        return false
    }

    fun backMoney(give_money:String,receipt: MutableList<String>,desc:String) {
        model.backMoney(pact_id,id,give_money, listToString(receipt),desc, object : GetBase {
            override fun getInfo(base: BaseBean) {
                setBaseValue(base)
            }

            override fun onError(e: String) {
            }
        })
    }
}